{% extends 'custom_admin/base.html' %}

{% block content %}
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">
            <i class="fas fa-comments me-2"></i>{{ title }}
            <span class="badge bg-success ms-2">{{ total_count }} 条评论</span>
        </h5>
        <form method="get" class="d-flex">
            <input type="text" name="search" value="{{ search }}" 
                   class="form-control search-box me-2" placeholder="搜索评论内容...">
            <button type="submit" class="btn btn-outline-primary">
                <i class="fas fa-search"></i>
            </button>
        </form>
    </div>
    
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th width="8%">ID</th>
                        <th width="15%">用户</th>
                        <th width="40%">评论内容</th>
                        <th width="20%">产品</th>
                        <th width="12%">时间</th>
                        <th width="5%">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for comment in comments %}
                    <tr>
                        <td><span class="badge bg-secondary"># {{ comment.id }}</span></td>
                        <td>{{ comment.user.username }}</td>
                        <td>{{ comment.content|truncatechars:80 }}</td>
                        <td>{{ comment.product.name|truncatechars:30 }}</td>
                        <td>{{ comment.create_time|date:"Y-m-d H:i" }}</td>
                        <td>
                            <button onclick="deleteComment({{ comment.id }})" 
                                    class="btn btn-outline-danger btn-sm" title="删除评论">
                                <i class="fas fa-trash"></i>
                            </button>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center py-4">
                            <div class="text-muted">
                                <i class="fas fa-inbox fa-3x mb-3 d-block"></i>
                                {% if search %}没有找到相关评论{% else %}暂无评论数据{% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        
        <!-- 分页 -->
        {% if page_obj.has_other_pages %}
        <div class="card-footer">
            <nav aria-label="评论分页">
                <ul class="pagination justify-content-center mb-0">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}&search={{ search }}">上一页</a>
                        </li>
                    {% endif %}
                    
                    <li class="page-item active">
                        <span class="page-link">{{ page_obj.number }}</span>
                    </li>
                    
                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}&search={{ search }}">下一页</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function deleteComment(commentId) {
    if (confirm('确定要删除这条评论吗？')) {
        $.post(`/custom-admin/api/delete-comment/${commentId}/`, {
            csrfmiddlewaretoken: '{{ csrf_token }}'
        })
        .done(function(data) {
            if (data.success) {
                alert(data.message);
                location.reload();
            } else {
                alert('删除失败: ' + data.message);
            }
        })
        .fail(function() {
            alert('网络错误，请重试');
        });
    }
}
</script>
{% endblock %}